package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :ttm
 * @description :
 */
@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private LogService logService;

    @Override
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr) {
        Gson gson = new Gson();
        List<PurchaseListGoods> purchaseListGoodsList = gson.fromJson(purchaseListGoodsStr, new TypeToken<List<PurchaseListGoods>>() {
        }.getType());
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        purchaseList.setUserId(currentUser.getUserId());
        purchaseListGoodsDao.savePurchaseList(purchaseList);
        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {

            purchaseListGoods.setPurchaseListId(purchaseList.getPurchaseListId());
            purchaseListGoods.setGoodsTypeId(goodsDao.findByGoodsId(purchaseListGoods.getGoodsId()).getGoodsTypeId());
            purchaseListGoodsDao.savePurchaseListGoods(purchaseListGoods);
            Goods goods = goodsDao.findByGoodsId(purchaseListGoods.getGoodsId());
            goods.setLastPurchasingPrice(purchaseListGoods.getPrice());
            goods.setInventoryQuantity(goods.getInventoryQuantity() + purchaseListGoods.getGoodsNum());
            goods.setPurchasingPrice(purchaseListGoods.getPrice() + goods.getPurchasingPrice());
            goods.setState(2);
            goodsDao.updateGoods(goods);

        }
        // 保存日志
        logService.save(new Log(Log.INSERT_ACTION, "新增进货单：" + purchaseList.getPurchaseNumber()));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        List<PurchaseList> purchaseListGoodsList = purchaseListGoodsDao.getPurchaseList(purchaseNumber, supplierId, state, sTime, eTime);
        map.put("rows", purchaseListGoodsList);
        logService.save(new Log(Log.SELECT_ACTION, "进货单据查询"));
        return map;

    }

    @Override
    public Map<String, Object> goodsList(Integer purchaseListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao.getPurchaseListGoodsByPurchaseListId(purchaseListId);
        map.put("rows", purchaseListGoodsList);
        logService.save(new Log(Log.SELECT_ACTION, "进货单商品信息"));
        return map;
    }

    @Override
    public ServiceVO delete(Integer purchaseListId) {
        logService.save(new Log(Log.DELETE_ACTION, "删除进货单：" + purchaseListGoodsDao.getPurchaseListById(purchaseListId)));

        purchaseListGoodsDao.deletePurchaseListGoodsByPurchaseListId(purchaseListId);

        purchaseListGoodsDao.deletePurchaseListById(purchaseListId);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
